#!/usr/bin/python
# -*- coding: utf-8 -*-
# @name             : YAML文件读写
# @author           : OneYoung
# @create           : 2024/02/04
# @version          : v1.0
# @desc             :

import yaml  # pip install pyyaml


def yaml_get(file_path, key=None):
    """
    读取YAML文件中的数据信息
    :param file_path: 文件路径
    :param key: 数据名称，为空则返回全部键值对
    """
    with open(file_path, mode='r', encoding='utf-8') as f:
        values = yaml.safe_load(f.read())
        values = values if values else {}
        value = values.get(key) if key else values
        return value


def yaml_set(file_path, data: dict):
    """
    保存数据信息到YAML文件
    :param file_path: 文件路径
    :param data: 键值对数据信息
    """
    values = yaml_get(file_path)
    values.update(data)
    with open(file_path, mode='w', encoding='utf-8') as f:
        yaml.safe_dump(values, stream=f, allow_unicode=True)
